#！/usr/bin/env python
#!coding=utf-8
import sys
import os
from werkzeug.wrappers import Request, Response
from flask import Flask, url_for, render_template

# 创建一个应用对象，名字为first_flask_demo
app1 = Flask('first_flask_demo')
@app1.route('/')                # 给app1添加处理函数，其对于URL是/
def flask_demo():
    return 'Welcome to python in one'

@app1.route('/api')        # 定义另外一个路由
def api_demo2():
    return 'you are visiting api_demo2'

@app1.route('/user/<username>')        # 指定了格式
def demo_param(username):                    # username是用户从地址上的输入
    return 'Welcome %s' % username

@Request.application                            # 定义一个处理函数
def demo1(request):
    return Response('Welcome to python in flask!')    # 返回html文档的内容

@app1.route('/hello/')
def index():
    #蛮有意思的这个hello.html默认应该在templates目录里面
    return render_template('hello.html')

@app1.route('/hello/<user_name>')
def hello_user(user_name):
    #蛮有意思的这个hello.html默认应该在templates目录里面
    return render_template("userhello.html", username = user_name)

@app1.route('/backforward')
def index_template():
    data = [
        {"href":"back.html", "caption":"back"},
        {"href":"forward.html", "caption":"forward"},
    ]
    return render_template('backward.html', href_list=data)



def test_werk():
    from werkzeug.serving import run_simple
    run_simple('localhost', 8086, demo1)        # 运行在8086端口上

def run():
    app1.run(debug=True)
if __name__ == '__main__':                        
    #test_werk()
    run()